﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Wpf.Ui.Controls;

namespace ClientAppraise.Controls
{
    public class ZxSnackbarPresenter : SnackbarPresenter
    {
        public new StackPanel Content
        {
            get
            {
                var aa = (StackPanel)GetValue(ContentProperty);
                if (aa == default)
                {
                    aa = new StackPanel() { VerticalAlignment = System.Windows.VerticalAlignment.Bottom, Margin = new System.Windows.Thickness(24) };
                    SetValue(ContentProperty, aa);
                }
                return aa;
            }
            protected set => SetValue(ContentProperty, value);
        }
        public override void AddToQue(Snackbar snackbar)
        {
            Queue.Enqueue(snackbar);

            if (Content is null)
                MyShowQueuedSnackbars();
        }

        public override Task ImmediatelyDisplay(Snackbar snackbar)
        {
            MyShowSnackbar(snackbar);

            MyShowQueuedSnackbars();
            return Task.CompletedTask;
        }

        public async override Task HideCurrent()
        {
            if (Content.Children.Count < 1) return;

            await MyHidSnackbar((Snackbar)Content.Children[Content.Children.Count - 1]);
        }

        private void MyShowQueuedSnackbars()
        {
            while (Queue.Count > 0 && !CancellationTokenSource.IsCancellationRequested)
            {
                var snackbar = Queue.Dequeue();
                MyShowSnackbar(snackbar);
            }
        }

        private void MyShowSnackbar(Snackbar snackbar)
        {
            if (!App.Current.Dispatcher.CheckAccess())
            {
                App.Current.Dispatcher.Invoke(new Action(() => MyShowSnackbar(snackbar)));
                return;
            }
            snackbar.Name = "a" + Guid.NewGuid().ToString("N");
            Content.Children.Add(snackbar);
            snackbar.IsShown = true;

            var Timeout = snackbar.Timeout;
            try
            {
                _ = Task.Run(async () =>
                {
                    try
                    {
                        await Task.Delay(Timeout);

                        await MyHidSnackbar(snackbar);
                    }
                    catch 
                    {
                        return;
                    }
                });
            }
            catch 
            {
                return;
            }


        }

        private async Task MyHidSnackbar(Snackbar snackbar)
        {
            if (!App.Current.Dispatcher.CheckAccess())
            {
                _ = App.Current.Dispatcher.Invoke(new Func<Task>(async () => await MyHidSnackbar(snackbar)));
                return;
            }
            snackbar.IsShown = false;
            await Task.Delay(300);
            Content.Children.Remove(snackbar);
        }
    }

}
